import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import Login from "../views/LoginView.vue";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: { path: "/login" },
  },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: "/singlePhone",
    name: "singlePhone",
    component: () => import("../views/SinglePhoneView.vue"),
  },
  {
    path: "/phoneList",
    name: "phoneList",
    component: () => import("../views/PhoneList.vue"),
  },
  {
    path: "/view",
    name: "viewPhone",
    component: () => import("../components/phoneViewComp.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.name === "/login") {
    if (sessionStorage.getItem("channelType") === "WebSocket") {
      sessionStorage.removeItem("cloudPhoneHost");
      sessionStorage.removeItem("token");
    }
    next("/login");
  } else {
    next();
  }
});

export default router;
